package com.zhao.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.pojo.Announcement;
import com.zhao.pojo.User;
import com.zhao.pojo.result.Response;
import com.zhao.pojo.result.ResponseResult;
import com.zhao.service.AnnouncementService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author : Mr.zhao
 * @Date: 2022/05/01/22:37
 * @Description:
 */
@Controller
@RequestMapping("/announcement")
public class AnnouncementController {

    @Autowired
    AnnouncementService announcementService;

    @RequestMapping("/data_announcement")
    public String data_announcement(
                               @RequestParam(value = "title",required = false)String title,
                               Model model) {
        QueryWrapper<Announcement> wrapper = new QueryWrapper<>();
        wrapper.like(Objects.nonNull(title),"title",title);
        wrapper.like(Objects.nonNull(title),"content",title);
        wrapper.orderByDesc("create_time");
        List<Announcement> list = announcementService.list(wrapper);
        model.addAttribute("lists",list);
        return "admin/data_announcement";
    }
    @RequestMapping("/addAnnouncement")
    public String  addAnnouncement(){
        return "admin/add_announcement";
    }
    @GetMapping("/editAnnouncement/{id}")
    public String editAnnouncement(@PathVariable("id")Integer id,Model model){
        Announcement announcement = announcementService.getById(id);
        model.addAttribute("announcement",announcement);
        return "admin/update_announcement";
    }
    @ResponseBody
    @RequestMapping("/updateAnnouncement")
    public ResponseResult<Integer> updateAnnouncement(Announcement announcement){
        if (Objects.isNull(announcement)){
            return Response.makeErrRsp("数据异常");
        }else if (Objects.isNull(announcement.getId())){
            announcement.setIsShow(1);
        }
        if (!announcementService.saveOrUpdate(announcement)) {
            return Response.makeErrRsp("修改失败");
        }
        return Response.makeOKRsp(announcement.getId());
    }
    @GetMapping("/delAnnouncement/{id}")
    public String delAnnouncement(@PathVariable("id")Integer id){
        announcementService.removeById(id);
        return "redirect:/announcement/data_announcement";
    }
    @RequestMapping(value = "/delAnnouncement",produces = "application/json;charset=UTF-8")
    public ResponseResult<Integer> delUser(@RequestBody String jsonParam){
        List<Announcement> announcementList = JSONArray.parseArray(jsonParam, Announcement.class);

        if (announcementList.size() == 0){
            return Response.makeErrRsp("请选择数据");
        }
        List<Integer> ids=new ArrayList<>();
        for (Announcement announcement : announcementList) {
            ids.add(announcement.getId());
        }
        if (!announcementService.removeByIds(ids)) {
            return Response.makeErrRsp("修改失败");
        }
        return Response.makeOKRsp("成功删除");
    }
    //isLock 修改
    @ResponseBody
    @RequestMapping("/updateIsLock/{id}/{isLock}")
    public ResponseResult<String> updateIsLock(@PathVariable Integer id, @PathVariable Integer isLock){
        Announcement announcement = new Announcement();
        announcement.setId(id).setIsShow(isLock);

        if (isLock==0){
            //禁用其他
            for (Announcement fid : announcementService.list(new QueryWrapper<Announcement>().eq("is_show", 0))) {
                announcementService.updateById(fid.setIsShow(1));
            }
            announcementService.updateById(announcement);
            return Response.makeOKRsp("已启用");
        }else {
            announcementService.updateById(announcement);
            return Response.makeOKRsp("已禁用");
        }
    }
}
